package com.blb.javatest01.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.javatest01.entity.Order;
import com.blb.javatest01.entity.ResponseResult;
import com.blb.javatest01.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@CrossOrigin
@RestController
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @GetMapping("/order-page")
    public ResponseResult<Page<Order>> getOrderPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                    @RequestParam(required = false,defaultValue = "5") Long size){
        IPage<Order> page = orderService.getOrdersPage(current, size);
        return ResponseResult.ok(page);
    }

    @PostMapping("/order")
    public ResponseResult<String> addOrder(@RequestBody Order order){
        orderService.save(order);
        return ResponseResult.ok("ok");
    }

    @PutMapping("/order")
    public ResponseResult<String> updateOrder(@RequestBody Order order){
        orderService.updateById(order);
        return ResponseResult.ok("ok");
    }

    @DeleteMapping("/order/{id}")
    public ResponseResult<String> deleteOrder(@PathVariable Long id){
        orderService.removeById(id);
        return ResponseResult.ok("ok");
    }
}
